package com.jms.web.controller.jms.service.impl;

import com.jms.common.filter.utils.DateUtils;
import com.jms.common.filter.utils.SecurityUtils;
import com.jms.web.controller.jms.domain.AttachFile;
import com.jms.web.controller.jms.domain.News;
import com.jms.web.controller.jms.mapper.AttachFileMapper;
import com.jms.web.controller.jms.mapper.NewsMapper;
import com.jms.web.controller.jms.service.INewsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 资讯详情Service业务层处理
 * 
 * @author drx
 * @date 2024-04-29
 */
@Service
public class NewsServiceImpl implements INewsService
{
    @Resource
    private NewsMapper newsMapper;
    @Resource
    private AttachFileMapper attachFileMapper;

    /**
     * 查询资讯详情
     * 
     * @param id 资讯详情主键
     * @return 资讯详情
     */
    @Override
    public News selectNewsById(Long id)
    {
        News news = newsMapper.selectNewsById(id);
        if (null != news){
            AttachFile attachFile = new AttachFile();
            attachFile.setSourceId(news.getId());
            // 文件类型 资讯详情
            attachFile.setFileClass("02");
            List<AttachFile> attachFiles = attachFileMapper.selectAttachFileList(attachFile);
            news.setAttachFiles(attachFiles);
        }
        return news;
    }

    /**
     * 查询资讯详情列表
     * 
     * @param news 资讯详情
     * @return 资讯详情
     */
    @Override
    public List<News> selectNewsList(News news)
    {
        return newsMapper.selectNewsList(news);
    }

    /**
     * 新增资讯详情
     * 
     * @param news 资讯详情
     * @return 结果
     */
    @Override
    public int insertNews(News news)
    {
        news.setCreateTime(DateUtils.getNowDate());
        news.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
        news.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        int i = newsMapper.insertNews(news);
        if (news.getAttachFiles() != null){
            news.getAttachFiles().forEach(bean->{
                bean.setFileClass("02");
                bean.setSourceId(news.getId());
                bean.setCreateTime(DateUtils.getNowDate());
                bean.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
                bean.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                attachFileMapper.insertAttachFile(bean);
            });
        }
        return i;
    }

    /**
     * 修改资讯详情
     * 
     * @param news 资讯详情
     * @return 结果
     */
    @Override
    public int updateNews(News news)
    {
        news.setUpdateTime(DateUtils.getNowDate());
        news.setUpdateName(SecurityUtils.getLoginUser().getUser().getNickName());
        news.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        attachFileMapper.deleteAttachFileBySourceId(news.getId());
        if (news.getAttachFiles() != null){
            news.getAttachFiles().forEach(bean->{
                bean.setFileClass("02");
                bean.setSourceId(news.getId());
                bean.setCreateTime(DateUtils.getNowDate());
                bean.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
                bean.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                attachFileMapper.insertAttachFile(bean);
            });
        }

        return newsMapper.updateNews(news);
    }

    /**
     * 批量删除资讯详情
     * 
     * @param ids 需要删除的资讯详情主键
     * @return 结果
     */
    @Override
    public int deleteNewsByIds(Long[] ids)
    {
        return newsMapper.deleteNewsByIds(ids);
    }

    /**
     * 删除资讯详情信息
     * 
     * @param id 资讯详情主键
     * @return 结果
     */
    @Override
    public int deleteNewsById(Long id)
    {
        return newsMapper.deleteNewsById(id);
    }
}
